

#include <config.h>




QString w2l_path;
QString whiteList;
bool read_slk;
bool del_lni;


QString config_file_path;
QString exe_path;

bool create_default_config()
{
    QFile config_file(config_file_path);

    if(config_file.open(QIODevice::WriteOnly))
    {
        config_file.write(QString("whiteList=%1\\whiteList.ini\n").arg(exe_path).toUtf8());
        config_file.write("read_slk=true\n");
        config_file.write("del_lni=false\n");
        cout<<"自动生成默认配置文件成功!"<<endl;
        cout<<"请配置好w2l.exe路径!"<<endl;
        return true;
    }
    else
    {
        cout<<"自动生成默认配置文件失败!"<<endl;
        return false;
    }
}




void load_config()
{
    QFile config_file(config_file_path);

    if(!config_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        cout<<"打开配置文件失败!"<<endl;
        if(!create_default_config()) return;
    }
    QRegExp rex_w2l("w2l=.+\\w2l\\.exe",Qt::CaseInsensitive);
    QRegExp rex_whiteList("whiteList=.+\\.ini",Qt::CaseInsensitive);
    QRegExp rex_slk("read_slk=true",Qt::CaseInsensitive);
    QRegExp rex_del_lni("del_lni=true",Qt::CaseInsensitive);
    QRegExp rex_del_n(".+\\n"); //去除行尾换行符
    while(!config_file.atEnd())
    {
       QByteArray line = config_file.readLine();
       if(rex_del_n.exactMatch(line))
       {
           line = line.left(line.length()-1);//去除换行符
       }
//       cout<< "line:"<< line.toStdString()<<endl;
       if(rex_w2l.exactMatch(line)){
            w2l_path = line.replace("w2l=","");
            continue;
       }
       if(rex_whiteList.exactMatch(line)){
            whiteList = line.replace("whiteList=","");
            continue;
       }
       if(rex_slk.exactMatch(line)){
           read_slk = true;
           continue;
       }
       if(rex_del_lni.exactMatch(line)){
            del_lni = true;
            continue;
       }
    }
    config_file.close();
//    cout<< w2l_path.toStdString() <<whiteList.toStdString() << read_slk <<del_lni<<endl;

}


QString gbk2Unicode(const char *s)
{
    QString str = QString::fromLocal8Bit(s);
    return str;
//    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
//    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
//    QString g2u = gbk->toUnicode(m_pUserSpi.m_mapInstruments.find(str1.toStdString())->second->InstrumentName);
}

bool write_all_config()
{
    QFile config_file(config_file_path);
    if(!config_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
       cout<<"打开配置文件失败!"<<endl;
       return false;
    }
    config_file.write(QString("w2l="+w2l_path+"\n").toUtf8());
    config_file.write(QString("whiteList="+whiteList+"\n").toUtf8());
    if(read_slk)
        config_file.write("read_slk=true\n");
    else
        config_file.write("read_slk=false\n");
    if(read_slk)
        config_file.write("del_lni=true\n");
    else
        config_file.write("del_lni=false\n");

    return true;
}

void config(QString cmd)
{
    QRegExp rex_w2l("w2l=.+\\w2l\\.exe",Qt::CaseInsensitive);
    QRegExp rex_whiteList("whiteList=.+\\.ini",Qt::CaseInsensitive);
    QRegExp rex_slk("(read_slk=true)|(read_slk=false)",Qt::CaseInsensitive);
    QRegExp rex_del_lni("(del_lni=true)|(del_lni=false)",Qt::CaseInsensitive);
    QRegExp rex_true(".*=true");

    if(rex_w2l.exactMatch(cmd))
    {   w2l_path = cmd.replace("w2l=","");
        write_all_config();
        return;
    }
    if(rex_whiteList.exactMatch(cmd))
    {
        whiteList = cmd.replace("whiteList=","");
        write_all_config();
        return;
    }
    if(rex_slk.exactMatch(cmd))
    {
        if(rex_true.exactMatch(cmd)) read_slk = true;
        else read_slk = false;

        write_all_config();
        return;
    }

    if(rex_del_lni.exactMatch(cmd))
    {
        if(rex_true.exactMatch(cmd)) del_lni = true;
        else del_lni = false;
        write_all_config();
        return;
    }

    cout<<"无效命令:"<<cmd.toStdString()<<error_text.toStdString()<<endl;
}

void show_config()
{
    QFile config_file(config_file_path);
    if(!config_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        cout<<"打开配置文件失败!"<<endl;
        if(!create_default_config()) return;
    }
    cout<< "当前配置信息:\n" << endl;
    while(!config_file.atEnd())
    {
        QByteArray line = config_file.readLine();
        cout<< line.toStdString() <<endl;
    }
}
void init_config()
{
    exe_path = QCoreApplication::applicationDirPath();
    exe_path.replace("/","\\");
    config_file_path = exe_path+"\\config.ini";
//    cout << "config_file_path" << config_file_path.toStdString() <<endl;
    load_config();
}

